package com.petvc.tv

import android.util.Log

object LogUtils {
    private const val TAG = "PetTV"
    
    // 网络请求相关日志
    private const val NETWORK_TAG = "PetTV_Network"
    
    // 认证相关日志
    private const val AUTH_TAG = "PetTV_Auth"
    
    // 网络安全相关日志
    private const val SECURITY_TAG = "PetTV_Security"
    
    // 通用日志
    fun d(message: String) {
        Log.d(TAG, message)
    }
    
    fun i(message: String) {
        Log.i(TAG, message)
    }
    
    fun w(message: String) {
        Log.w(TAG, message)
    }
    
    fun e(message: String, throwable: Throwable? = null) {
        Log.e(TAG, message, throwable)
    }
    
    // 网络请求日志
    fun logNetwork(message: String) {
        Log.d(NETWORK_TAG, "🌐 $message")
    }
    
    fun logNetworkRequest(url: String, method: String, headers: Map<String, String>? = null, body: String? = null) {
        val log = StringBuilder()
        log.append("📤 请求详情:\n")
        log.append("   URL: $url\n")
        log.append("   方法: $method\n")
        
        headers?.let {
            log.append("   请求头:\n")
            it.forEach { (key, value) ->
                log.append("     $key: $value\n")
            }
        }
        
        body?.let {
            log.append("   请求体: $it\n")
        }
        
        Log.d(NETWORK_TAG, log.toString())
    }
    
    fun logNetworkResponse(url: String, statusCode: Int, responseBody: String, headers: Map<String, String>? = null) {
        val log = StringBuilder()
        log.append("📥 响应详情:\n")
        log.append("   URL: $url\n")
        log.append("   状态码: $statusCode\n")
        
        headers?.let {
            log.append("   响应头:\n")
            it.forEach { (key, value) ->
                log.append("     $key: $value\n")
            }
        }
        
        log.append("   响应体: $responseBody\n")
        
        Log.d(NETWORK_TAG, log.toString())
    }
    
    fun logNetworkError(url: String, error: String, throwable: Throwable? = null) {
        val log = StringBuilder()
        log.append("❌ 网络错误:\n")
        log.append("   URL: $url\n")
        log.append("   错误: $error\n")
        
        throwable?.let {
            log.append("   异常: ${it.message}\n")
        }
        
        Log.e(NETWORK_TAG, log.toString(), throwable)
    }
    
    // 认证相关日志
    fun logAuth(message: String) {
        Log.d(AUTH_TAG, "🔐 $message")
    }
    
    fun logLogin(username: String, success: Boolean, message: String) {
        val status = if (success) "✅" else "❌"
        Log.d(AUTH_TAG, "$status 登录结果: 用户=$username, 成功=$success, 消息=$message")
    }
    
    fun logUserData(userData: UserData?) {
        if (userData != null) {
            Log.d(AUTH_TAG, "👤 用户数据: ID=${userData.id}, 用户名=${userData.username}")
        } else {
            Log.d(AUTH_TAG, "👤 用户数据: null")
        }
    }
    
    fun logToken(token: String?) {
        if (token != null) {
            Log.d(AUTH_TAG, "🎫 令牌: ${token.take(20)}...")
        } else {
            Log.d(AUTH_TAG, "🎫 令牌: null")
        }
    }
    
    // 调试信息
    fun logDebug(message: String) {
        Log.d(TAG, "🐛 $message")
    }
    
    // 性能监控
    fun logPerformance(operation: String, duration: Long) {
        Log.d(TAG, "⏱️ $operation 耗时: ${duration}ms")
    }
    
    // 网络安全日志
    fun logSecurity(message: String) {
        Log.d(SECURITY_TAG, "🔒 $message")
    }
    
    fun logSecurityError(message: String, throwable: Throwable? = null) {
        Log.e(SECURITY_TAG, "🔒 安全错误: $message", throwable)
    }
} 